/*初始化数据表格*/
function initTable() {
    var url=getBasePath()+'/mdm/dealer/getListData.json';
    var tableId="table_data";
    $("#"+tableId).dataTable().fnDestroy();
    json = {};
    json.showLineNuber = false;
    json.id = tableId;
    json.pagesize=2;
    json.url = url ;
    json.param = [
        {"name": "dealerId", "value": $.trim($("#dealerId").val())},
        {"name": "companyCode", "value": $.trim($("#companyCode").val())},
        {"name": "dealerCode", "value": $.trim($("#dealerCode").val())},
        {"name": "dealerName", "value": $.trim($("#dealerName").val())},
        {"name": "nationCode", "value": $.trim($("#nationCode").val())},
        {"name": "provinceCode", "value": $.trim($("#provinceCode").val())},
        {"name": "cityCode", "value": $.trim($("#cityCode").val())},
        {"name": "districtCode", "value": $.trim($("#districtCode").val())},
        {"name": "street", "value": $.trim($("#street").val())},
        {"name": "address", "value": $.trim($("#address").val())},
        {"name": "postcode", "value": $.trim($("#postcode").val())},
        {"name": "contact", "value": $.trim($("#contact").val())},
        {"name": "mobiPhone", "value": $.trim($("#mobiPhone").val())},
        {"name": "tel", "value": $.trim($("#tel").val())},
        {"name": "fax", "value": $.trim($("#fax").val())},
        {"name": "latitude", "value": $.trim($("#latitude").val())},
        {"name": "longitude", "value": $.trim($("#longitude").val())},
        {"name": "display", "value": $.trim($("#display").val())},
    ];
    json.columns = [
        {
            "mData": "dealerCode", "sWidth": "15%", "sClass": "text-center",
            "mRender": function (data, type, row) {
                if (data) {
                    var editStr="<a href=\"#\"  data-am-popover=\"{theme: 'primary sm', content:'修改', trigger: 'hover focus'}\" onclick='toEditDealer("+data+");'><i class=\"icon-edit\"></i></a>";
                    var delStr="<a href=\"#\" data-am-popover=\"{theme: 'primary sm', content:'删除', trigger: 'hover focus'}\"  data-am-modal=\"{target: '#del-confirm', closeViaDimmer: 0, width: 400}\" onclick='deleteDealer("+data+");'><i class=\"icon-bin\"></i></a>";

                    return editStr+delStr;
                }
            }
        },
        {"mData": "dealerId", "sClass": "text-center"},
        {"mData": "companyCode", "sClass": "text-center"},
        {"mData": "dealerCode", "sClass": "text-center"},
        {"mData": "dealerName", "sClass": "text-center"},
        {"mData": "nationCode", "sClass": "text-center"},
        {"mData": "provinceCode", "sClass": "text-center"},
        {"mData": "cityCode", "sClass": "text-center"},
        {"mData": "districtCode", "sClass": "text-center"},
        {"mData": "street", "sClass": "text-center"},
        {"mData": "address", "sClass": "text-center"},
        {"mData": "postcode", "sClass": "text-center"},
        {"mData": "contact", "sClass": "text-center"},
        {"mData": "mobiPhone", "sClass": "text-center"},
        {"mData": "tel", "sClass": "text-center"},
        {"mData": "fax", "sClass": "text-center"},
        {"mData": "latitude", "sClass": "text-center"},
        {"mData": "longitude", "sClass": "text-center"},
        {"mData": "display", "sClass": "text-center"},
    ];
    var oTable1 = TableManaged(json).init();
}
/*查询数据*/
function doQuery(){
    initTable();
}

/*重置查询条件*/
function resetQueryForm(){
	$("#dealerId").val('');
	$("#companyCode").val('');
	$("#dealerCode").val('');
	$("#dealerName").val('');
	$("#nationCode").val('');
	$("#provinceCode").val('');
	$("#cityCode").val('');
	$("#districtCode").val('');
	$("#street").val('');
	$("#address").val('');
	$("#postcode").val('');
	$("#contact").val('');
	$("#mobiPhone").val('');
	$("#tel").val('');
	$("#fax").val('');
	$("#latitude").val('');
	$("#longitude").val('');
	$("#display").val('');
}

/*删除数据*/
function deleteArea(dealerCode){
    var _url=getBasePath()+'/mdm/dealer/delete.htm';
    $.Confirm('你确定要删除这条记录吗？','',function(){
        var dealer={};
        dealer.dealerCode=dealerCode;
        $.ajax({
            url:_url,
            type:'POST',
            dataType:'json',
            contentType:'application/json',
            data:JSON.stringify(dealer),
            success:function(data){
                if(data.status==200){
                    $.Success('删除成功',doQuery);
                }else{
                    var msg = data.message || "删除经销商数据发生错误";
                    $.Error(msg,'',null);
                }
            }
        });
    });

}

/*跳转编辑数据页面*/
function toEditArea(dealerCode){
    var _url=getBasePath()+'/mdm/dealer/toEdit.htm?dealerCode='+dealerCode;
    location.href=_url;
}

/*编辑数据*/
function editArea(){
    $validator =$("#dealer_edit_form").validator('isFormValid');
    if(!$validator){
        return;
    }
    var _url=getBasePath()+'/mdm/dealer/edit.htm';
    var dealer={};
	dealer.dealerId=$("#dealerId").val();
	dealer.companyCode=$("#companyCode").val();
	dealer.dealerCode=$("#dealerCode").val();
	dealer.dealerName=$("#dealerName").val();
	dealer.nationCode=$("#nationCode").val();
	dealer.provinceCode=$("#provinceCode").val();
	dealer.cityCode=$("#cityCode").val();
	dealer.districtCode=$("#districtCode").val();
	dealer.street=$("#street").val();
	dealer.address=$("#address").val();
	dealer.postcode=$("#postcode").val();
	dealer.contact=$("#contact").val();
	dealer.mobiPhone=$("#mobiPhone").val();
	dealer.tel=$("#tel").val();
	dealer.fax=$("#fax").val();
	dealer.latitude=$("#latitude").val();
	dealer.longitude=$("#longitude").val();
	dealer.display=$("#display").val();

    $.ajax({
        url:_url,
        type:'POST',
        dataType:'json',
        contentType:'application/json',
        data:JSON.stringify(dealer),
        success:function(data){
            if(data.status==200){
                $.Success('该记录已提交成功!',gotoListPage);
            }else{
                var msg = data.message || "编辑经销商数据发生错误";
                $.Error(msg,'',null);
            }

        },
        error:function(data){
            JSON.stringify(data);
        }
    });
}

/*跳转新增数据页面*/
function toAddArea(){
    var _url=getBasePath()+'/mdm/dealer/toAdd.htm';
    location.href=_url;
}

/*新增数据*/
function saveArea(){
    $validator =$("#dealer_add_form").validator('isFormValid');
    if(!$validator){
        return;
    }
    var _url=getBasePath()+'/mdm/dealer/add.htm';
    var dealer={};
	dealer.dealerId=$("#dealerId").val();
	dealer.companyCode=$("#companyCode").val();
	dealer.dealerCode=$("#dealerCode").val();
	dealer.dealerName=$("#dealerName").val();
	dealer.nationCode=$("#nationCode").val();
	dealer.provinceCode=$("#provinceCode").val();
	dealer.cityCode=$("#cityCode").val();
	dealer.districtCode=$("#districtCode").val();
	dealer.street=$("#street").val();
	dealer.address=$("#address").val();
	dealer.postcode=$("#postcode").val();
	dealer.contact=$("#contact").val();
	dealer.mobiPhone=$("#mobiPhone").val();
	dealer.tel=$("#tel").val();
	dealer.fax=$("#fax").val();
	dealer.latitude=$("#latitude").val();
	dealer.longitude=$("#longitude").val();
	dealer.display=$("#display").val();
    
    $.ajax({
        url:_url,
        type:'POST',
        dataType:'json',
        contentType:'application/json',
        data:JSON.stringify(dealer),
        success:function(data){
            if(data.status==200){
                $.Success('该记录已提交成功!',gotoListPage);
            }else{
                var msg = data.message || "新增经销商数据发生错误";
                $.Error(msg,'',null);
            }
        }
    });
}

/*跳转数据列表页面*/
function gotoListPage(){
    location.href=getBasePath()+'/mdm/dealer/list.htm';
}





